package second;

import java.util.Arrays;
import java.util.Scanner;

public class Main3 {
	public static void main(String[] args)
	{
		Scanner sc=new Scanner(System.in);
		int n=Integer.parseInt(sc.nextLine());
		String[] ids=new String[n];
		for(int i=0;i<n;i++)
		{
			ids[i]=sc.nextLine();
		}
		while(true)
		{
		String choice=sc.next();
		String[] year=new String[n];
		String[] month=new String[n];
		String[] date=new String[n];
		String[] str=new String[n];
		for (int i = 0; i < n; i++) {
			year[i]=ids[i].substring(6,10);
			month[i]=ids[i].substring(10,12);
			date[i]=ids[i].substring(12,14);
			str[i]=year[i]+month[i]+date[i];
		}
		int[] digits=new int[str.length];
		for (int i = 0; i < str.length; i++) {
			digits[i]=Integer.parseInt(str[i]);
		}
		Arrays.sort(digits);
		if(choice.equals("sort1"))
		{
			String[] s=new String[n];
			for (int i = 0; i < s.length; i++) {
				s[i]=Integer.toString(digits[i]);
				System.out.println(s[i].substring(0, 4)+"-"+s[i].substring(4,6)+"-"+s[i].substring(6,8));
			}
		}
		else if(choice.equals("sort2"))
		{
			String[] t=new String[n];
			for (int i = 0; i < t.length; i++) {
				t[i]=Integer.toString(digits[i]);
				for (int j = 0; j <n; j++) {
					if(t[i].equals(ids[j].substring(6, 14)))
					{
						System.out.println(ids[j]);
					}
				}
			}
		}
		else
			System.out.println("exit");
	}
	}
}